<?php
/**
 * 处理提交的公共网关
 *
 * Created		: 2009-06-03
 * Modified		: 2011-09-12
 * @link		: http://www.topews.com
 * @copyright	: (c) 2009-2011
 * @version	: 0.1.0
 * @author		: Joseph Chen (Jsph.chen@gmail.com)
 */

define('DESC_CONTENT', $GLOBALS['tbl_pre'].'desc_content');

if (empty($_POST['do']) && empty ($_GET['do']))
{
	$g_msg = array(
		'type' => 'Error',
		'desc' => $GLOBALS['ActionValueIsEmpty'],
	);
	return false;
}
$do = empty($_POST['do']) ? $_GET['do'] : $_POST['do'];

if (empty($_GET['domo'])) {
	$domo = $do;
	if (is_file($GLOBALS['app_admin'].'/action/'.$domo.'.php')) {
		$commonDo = false;
	} else {
		$commonDo = true;
	}
} else {
	$commonDo = false;
}
if ($commonDo) {
	switch ($do)
	{
		case 'phpinfo':
			phpinfo();
			exit;
		case 'gethostbyname':
			if (empty($_GET['hostname']))
			{
				echo '';
			} else {
				echo gethostbyname($_GET['hostname']);
			}
			exit;
		case 'logout':
			$_SESSION['is_admin'] = false;
			$r_url = './admin.php?mo=login';
			$type = 'Success';
			$desc = $GLOBALS['LSuccess'];
			break;
		case 'switch_lang':
			$dir = $app_lang.$_POST['lang'];
			if (is_dir($dir)) {
				$type = 'true';
			} else {
				$type = 'false';
			}
			$type = 'Success';
			$desc = $GLOBALS['LSuccess'];
			exit;
		case 'modifyAdmin':
			$newPass = App::modifyAdmin($_POST['username'], $_POST['password']);
			$type = 'Success';
			$desc = $GLOBALS['LSuccess'];
			$content = json_encode(array(
					'username'	=> $_POST['username'],
					'password'	=> $_POST['password'],
			));
			App::syslog('modifyAdmin', $content);
			$r_url = 'admin.php?mo=modifyAdmin';
			break;
		case 'smtpSet':
			App::smtpSet();
			$type = 'Success';
			$desc = $GLOBALS['LSuccess'];
			$content = json_encode(array(
					'smtp_ssl'	=> $_POST['smtp_ssl'],
					'smtp_host'	=> $_POST['smtp_host'],
					'smtp_port'	=> $_POST['smtp_port'],
					'smtp_email'=> $_POST['smtp_email'],
					'smtp_user'	=> $_POST['smtp_user'],
					'smtp_pass'	=> $_POST['smtp_pass'],
			));
			App::syslog('smtpSet', $content);
			$r_url = 'admin.php?mo=smtpSet';
			break;
		case 'websiteSet':
			$cfgFile = APP_ROOT.'Config'.DS.'site.cfg.php';
			$site = include($cfgFile);
			$site['siteName'] = $_POST['siteName'];
			Fso::write($cfgFile, 'return '.var_export($site, true), 'php');
			$type = 'Success';
			$desc = $GLOBALS['LSuccess'];
			$content = json_encode($site);
			App::syslog('smtpSet', $content);
			$r_url = 'admin.php?mo=websiteSet';
			break;
		// 关于我们
		case 'aboutUs':
			$param = array('aboutus', $_POST['lang']);
			$record = Db::getOne(DESC_CONTENT, 'sign=? and lang=?', 'id,content', $param);
			if ($record) {
				$data = array(
					'content'	=> $_POST['content']
				);
				$ret = Db::update(DESC_CONTENT, $data, 'id='.$record['id']);
			} else {
				$data = array(
					'content'	=> $_POST['content'],
					'sign'		=> 'aboutus',
					'lang'		=> $_POST['lang'],
				);
				$ret = Db::insert(DESC_CONTENT, $data);
			}
			$type = 'Success';
			$desc = $GLOBALS['LSuccess'];
			$r_url = 'admin.php?mo=aboutUs';
			break;
		// 联系我们
		case 'contactUs':
			$param = array('contactus', $_POST['lang']);
			$record = Db::getOne(DESC_CONTENT, 'sign=? and lang=?', 'id,content', $param);
			if ($record) {
				$data = array(
					'content'	=> $_POST['content']
				);
				$ret = Db::update(DESC_CONTENT, $data, 'id='.$record['id']);
			} else {
				$data = array(
					'content'	=> $_POST['content'],
					'sign'		=> 'contactus',
					'lang'		=> $_POST['lang'],
				);
				$ret = Db::insert(DESC_CONTENT, $data);
			}
			$type = 'Success';
			$desc = $GLOBALS['LSuccess'];
			$r_url = 'admin.php?mo=contactUs';
			break;
		// 联系我们
		case 'bottomInfo':
			$param = array('bottominfo', $_POST['lang']);
			$record = Db::getOne(DESC_CONTENT, 'sign=? and lang=?', 'id,content', $param);
			if ($record) {
				$data = array(
					'content'	=> $_POST['content']
				);
				$ret = Db::update(DESC_CONTENT, $data, 'id='.$record['id']);
			} else {
				$data = array(
					'content'	=> $_POST['content'],
					'sign'		=> 'bottominfo',
					'lang'		=> $_POST['lang'],
				);
				$ret = Db::insert(DESC_CONTENT, $data);
			}
			$type = 'Success';
			$desc = $GLOBALS['LSuccess'];
			$r_url = 'admin.php?mo=bottominfo';
			break;
//		case 'lock':
//			$_SESSION['lock'] = true;
//			break;
//		case 'unlock':
//			$cfgFile = APP_ROOT.'Config'.DS.'adminpw.cfg.php';
//			include($cfgFile);
//			$encodePassword = md5(md5($_POST['password']).md5($admin_private_key));
//			if ($encodePassword == $admin_password) {
//				$result = array('result' => true);
//			} else {
//				$result = array(
//					'result'	=> false,
//					'msg'		=> $GLOBALS['LAdminPasswordError']
//				);
//			}
//			$_SESSION['lock'] = false;
//			echo json_encode($result);
//			exit;
		default:
			$type = 'Error';
			$desc = $GLOBALS['LUnknowRequest'];
			$r_url = 'javascript:history.go(-1);';
			break;
	}
} else {
	$domo = $_GET['domo'];
	include $admin_root.'/action/'.$domo.'.php';
}

if (empty($g_msg)) {
	$g_msg = array(
		'type' => $type,
		'desc' => isset($desc) ? $desc : App::msg(),
	);
}

if (!empty($_GET['url'])) {
	header('refresh:2;url='.urldecode($_GET['url']));
} elseif (!empty($r_url)) {
	header('refresh:2;url='.$r_url);
}

$mo_tpl = 'msg';

return true;


